package com.cxt.controller;//package com.rds.controller.admin;


//import com.rds.constant.MessageConstant;
//import com.rds.result.Result;
//import com.rds.utils.AliOssUtil;
import com.cxt.result.Result;
import com.cxt.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/upload")
@Slf4j
public class UploadController {
    @Autowired
    private AliOssUtil aliOssUtil;
    @PostMapping("/image")
    public Result<String> upload(MultipartFile file) {
        log.info("文件上传");
        try {
            //获取文件名
            String originalFilename = file.getOriginalFilename();
            //截取文件后缀
            String ex = originalFilename.substring(originalFilename.lastIndexOf("."));
            //UUID生成文件名
            String objectName= UUID.randomUUID().toString()+ex;
            String fillPath = aliOssUtil.upload(file.getBytes(), objectName);
            return  Result.success(fillPath);
        } catch (IOException e) {
            return  Result.error("上传失败");
        }
    }
}
